import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/user/user_obj.dart';
import 'package:xws/ui/others/others_page.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_list_tile.dart';

/// 好友项
class FriendItem extends StatefulWidget {
  const FriendItem({
    Key? key,
    required this.user,
    this.refresh,
    this.isSearch = false,
  }) : super(key: key);

  @override
  _FriendItemState createState() => _FriendItemState();

  final User user;
  final bool isSearch;
  final Function()? refresh;
}

class _FriendItemState extends State<FriendItem> with SafeState<FriendItem> {
  User get _u => widget.user;

  /// 点击用户尾部按钮
  Future<void> _onTap() async {
    loading(context);

    if (_u.friendFlag) {
      final bool r = await delFriend(_u.id);
      if (r) {
        safeSetState(() => _u.friendFlag = false);
      }
    } else {
      final bool r = await addFriend(_u.id);
      if (r) {
        safeSetState(() => _u.friendFlag = true);
      }
    }

    // if (widget.isSearch) {

    // } else {
    //   final bool r = await delFriend(_u.id);
    //   if (r) {
    //     await widget.refresh?.call();
    //   }
    // }

    close(context);
  }

  /// 点击用户
  Future<void> _onTapUser() async {
    await push(context, OthersPage(user: _u, tapFollow: _onTap));
    await widget.refresh?.call();
  }

  @override
  Widget build(BuildContext context) {
    return AppListTile(
      onTap: _onTapUser,
      height: 64,
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
      leading: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: AppImg.fitCover(
          url: _u.avatarUrl,
          width: 44,
          height: 44,
          radius: 30,
        ),
      ),
      title: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: Row(
          children: <Widget>[
            Container(
              child: Text(
                _u.city,
                style: TextStyle(
                  fontSize: 10,
                  color: white(context),
                  height: 1.19,
                ),
              ),
              height: 17,
              padding: const EdgeInsets.symmetric(horizontal: 3),
              alignment: Alignment.center,
              margin: const EdgeInsets.only(right: 4),
              decoration: BoxDecoration(
                color: theme(context),
                borderRadius: BorderRadius.circular(2),
              ),
            ),
            Flexible(
              child: Text(
                _u.nickName,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                  color: black(context).withOpacity(0.6),
                ),
              ),
            ),
          ],
        ),
      ),
      subtitle: Text(
        _u.description,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 12,
          color: black(context).withOpacity(0.6),
        ),
      ),
      trailing: _buildTrailing,
    );
  }

  Widget get _buildTrailing {
    if (widget.isSearch) {
      return AppBtn(
        onTap: _onTap,
        text: _u.friendFlag ? '移除' : '关注',
        textStyle: _u.friendFlag
            ? TextStyle(
                color: theme(context),
              )
            : null,
        color: _u.friendFlag ? Colors.transparent : null,
        border: _u.friendFlag
            ? Border.all(
                color: theme(context),
              )
            : null,
      );
    }

    return AppBtn(
      onTap: _onTap,
      text: '移除',
      textStyle: TextStyle(
        color: theme(context),
      ),
      color: Colors.transparent,
      border: Border.all(
        color: theme(context),
      ),
    );
  }
}
